package problem978;

//978.最长湍流子数组
// https://leetcode.cn/problems/longest-turbulent-subarray/submissions/

class Solution {
    public int maxTurbulenceSize(int[] arr) {
        int n = arr.length;
        int[] f = new int[n];
        int[] g = new int[n];
        for(int i = 0; i<n; i++) {
            f[i] = g[i] = 1;
        }
        int ret = 1;
        for(int i = 1; i<n; i++) {
            if(arr[i-1] < arr[i]) {
                g[i] = f[i-1] + 1;
            }else if(arr[i-1] > arr[i]) {
                f[i] = g[i-1] + 1;
            }
            ret = Math.max(ret, Math.max(g[i], f[i]));
        }
        return ret;
    }
}

/*
f[i]: 以第i个元素结尾, 并且 arr[i-1] < arr[i] 的最长湍流子数组的长度
f[i] = g[i-1] + 1;
g[i]: 以第i个元素结尾, 并且 arr[i-1] > arr[i] 的最长湍流子数组的长度
g[i] = f[i-1] + 1;
*/